package com.shopping.mall.core.module.ipAddress.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.ipAddress.entity.IpAddress;
import com.shopping.mall.core.module.ipAddress.entity.IpAddressVO;
import com.shopping.mall.core.module.ipAddress.service.IpAddressService;
import com.shopping.mall.core.module.saleData.entity.SaleData;
import com.shopping.mall.core.module.saleData.entity.SaleDataQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Api(tags = "IpAddress")
@RestController
@RequestMapping("/ipAddress")
public class IpAddressController {
    @Autowired
    private IpAddressService ipAddressService;

    @UnLogin
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<IpAddress>> getIpAddressPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        IpAddressVO ipAddress) {
        IPage<IpAddress> ipAddressPage=ipAddressService.getIpAddressPage(current,size,ipAddress);
        return Result.OK(ipAddressPage);
    }

    @ApiOperation("修改")
//    @PutMapping
    public Result<IpAddress> update(@Validated(IpAddress.update.class) @RequestBody IpAddress ipAddress) {
        ipAddressService.updateById(ipAddress);
        return Result.OK();
    }

    @UnLogin
    @ApiOperation("新增(提供url和source)")
    @PostMapping
    public Result<Object> add(@Validated(IpAddress.insert.class) @RequestBody IpAddress ipAddress, HttpServletRequest request) {
        ipAddressService.add(ipAddress,request);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<IpAddress> getById(@RequestParam String id){
        IpAddress ipAddress=ipAddressService.getById(id);
        return Result.OK(ipAddress);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<IpAddress>> getAll(IpAddressVO ipAddress){
        List<IpAddress> ipAddressList=ipAddressService.getAll(ipAddress);
        return Result.OK(ipAddressList);
    }

    @ApiOperation("通过id删除")
//    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return ipAddressService.deleteById(id);
    }

    @ApiOperation("批量删除")
//    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return ipAddressService.batchDelete(ids);
    }

    @UnLogin
    @ApiOperation("导出数据")
    @GetMapping("/data/excel")
    public void getExcel(IpAddressVO ipAddressVO, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("ip访问统计", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<IpAddress> data=ipAddressService.getAll(ipAddressVO);
        EasyExcel.write(response.getOutputStream(), IpAddress.class).sheet("sheet1").doWrite(data);
    }
}
